package com.javademo;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.HashMap;

@Slf4j
public class Solution {
    public static void main(String[] args) {
        //String[] s = new String[]{"flower","flow","flight"};
        //String[] s = new String[]{"flower","flow","flight"};
        String s = "  hello world  ";
        String result = new Solution().reverseWords(s);
        log.info("result:{}", result);
    }

    public String reverseWords(String s) {
        StringBuilder sb = new StringBuilder();
        StringBuilder res = new StringBuilder();
        s = s.trim();
        boolean isSpace = true;
        for (int i = s.length() - 1; i >= 0; i--){
           if (s.charAt(i) != ' '){
               res.append(s.charAt(i));
               isSpace = false;
           }
           if (s.charAt(i) == ' ' || i == 0){
               if (!isSpace){
                   res.reverse();
                   sb.append(res);
                   if (i != 0){
                       sb.append(" ");
                   }
                   res.delete(0, res.length());
                   isSpace = true;
               }

           }
        }
        return sb.toString();
    }
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        Arrays.sort(strs);
        String st = strs[0];
        String en = strs[strs.length - 1];
        int i;
        int num = Math.min(st.length(), en.length());
        for (i = 0; i < num && st.charAt(i) == en.charAt(i); i++) ;
        String res = st.substring(0, i);
        return res;
    }

    public int lengthOfLastWord(String s) {
        int len = s.length() - 1;
        int count = 0;
        boolean foundWord = false;

        while (len >= 0) {
            char lastChar = s.charAt(len);
            if (lastChar == ' ') {
                if (foundWord) {
                    break;
                }
            } else {
                foundWord = true;
                count++;
            }
            len--;
        }
        return count;
    }

    public String intToRoman(int num) {
        switch(num){
            case 1: return "I";
            case 4: return "IV";
            case 5: return "V";
            case 9: return "IX";
            case 10: return "X";
            case 40: return "XL";
            case 50: return "L";
            case 90: return "XC";
            case 100: return "C";
            case 400: return "CD";
            case 500: return "D";
            case 900: return "CM";
            case 1000: return "M";
            default: return "error";
        }
    }

    public int romanToInt(String s) {
        int sum = 0;
        int preNum = getValue(s.charAt(0));
        for(int i = 1;i < s.length(); i ++) {
            int num = getValue(s.charAt(i));
            if(preNum < num) {
                sum -= preNum;
            } else {
                sum += preNum;
            }
            preNum = num;
        }
        sum += preNum;
        return sum;
    }
    private int getValue(char ch) {
        switch(ch) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            default: return 0;
        }
    }

    public int trap(int[] height){
        int left = 0;
        int right = height.length - 1;
        int leftMax = 0;
        int rightMax = 0;
        int capacity = 0;
        while (left < right){
            if (leftMax < rightMax){
                if (height[left] > leftMax){
                    leftMax = height[left];
                }else {
                    capacity += leftMax - height[left];
                }
                left++;
            }else {
                if (height[right] > rightMax){
                    rightMax = height[right];
                }else {
                    capacity += rightMax = height[right];
                }
                right--;
            }
        }
        return capacity;
    }
    public int candy(int[] ratings) {
        int n = ratings.length;
        int[] candies = new int[n];
        Arrays.fill(candies, 1);

        // 从左到右遍历
        for (int i = 1; i < n; i++) {
            if (ratings[i] > ratings[i - 1]) {
                candies[i] = candies[i - 1] + 1;
            }
        }

        // 从右到左遍历
        for (int i = n - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                candies[i] = Math.max(candies[i], candies[i + 1] + 1);
            }
        }

        // 计算总糖果数
        int totalCandies = 0;
        for (int candy : candies) {
            totalCandies += candy;
        }

        return totalCandies;
    }
    private static int canCompleteCircuit(int[] gas, int[] cost) {
        boolean begin = false;
        int start = 0;
        int capacity = 0;
        for (int i = 0; i < gas.length; i++){
            if (!begin){
                if (gas[i] >= cost[i]){
                    capacity += gas[i] - cost[i];
                    start = i;
                    begin = true;
                }
            }else {
                capacity += gas[i] - cost[i];
                if (capacity <= 0){
                    begin = false;
                }
            }
        }
        return begin ? start : -1;
    }
}
